/*
 * @Author: lihaogn
 * @Date: 2021-12-18 20:56:23
 * @LastEditTime: 2021-12-21 10:56:38
 * @LastEditor: lihaogn
 * @Description: 权限模型
 * @FilePath: \lix-admin-server\app\model\permission.js
 */
"use strict";
module.exports = (app) => {
  const { STRING, DATE, TINYINT } = app.Sequelize;

  // app.model 相当于 sequelize 实例
  const Permission = app.model.define(
    "permission",
    {
      id: {
        type: STRING(40),
        primaryKey: true,
      },
      name: {
        type: STRING(20),
        allowNull: false,
        unique: "uk_permission_name",
      },
      mark: {
        type: STRING(30),
        allowNull: false,
        unique: "uk_permission_mark",
      },
      type: {
        // 权限类型：1-页面、2-操作
        type: TINYINT,
        allowNull: false,
      },
      description: {
        type: STRING(30),
      },
      parentId: {
        type: STRING(40),
        field: "parent_id",
      },
      createdAt: { type: DATE, field: "created_at" },
      updatedAt: { type: "TIMESTAMP", field: "updated_at" },
    },
    {
      // 指定数据库中表的名字
      tableName: "permissions",
    }
  );

  Permission.associate = function () {
    app.model.Permission.belongsToMany(app.model.Role, {
      as: "role",
      through: "RolePermission",
    });
  };

  return Permission;
};
